﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using System;
using MbUnit.Framework;
using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;
using Gallio.Framework;
using DotNetNuke.Tests.UI.WatiN.Utilities;
using System.Configuration;
using WatiN.Core.DialogHandlers;
using DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects;

namespace DotNetNuke.Tests.UI.WatiN.SiteTests
{
    [TestFixture]
    [DependsOn(typeof(PageTests))]
    [DependsOn(typeof(UserAccountsTests))]
    public class RoleTests : STBase
    {
        #region Test Setup and TearDown

        [SetUp]
        public void SetUp()
        {
            SetUpTest();
        }

        [TearDown]
        public void TearDown()
        {
            TearDownTest();
        }

        #endregion

        #region Tests

        [Test]
        public void Add_New_Role_Group()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);

            //Act
            ieInstance.GoTo(SiteURL + "/Admin/SecurityRoles.aspx");
            SecurityRolesPage rolesPage = new SecurityRolesPage(homePage);
            rolesPage.AddNewRoleGroupLink.Click();
            rolesPage.RoleGroupNameField.Value = "Test Group";
            rolesPage.UpdateLink.Click();
            System.Threading.Thread.Sleep(1000);

            //Assert 
            Assert.IsTrue(rolesPage.FilterByGroupSelectList.Option("Test Group").Exists);
        }

        [Test]
        [DependsOn("Add_New_Role_Group")]
        public void Delete_Role_Group()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            String roleGroup = "Test Group";

            //Create a role group to be deleted 
            ieInstance.GoTo(SiteURL + "/Admin/SecurityRoles.aspx");
            SecurityRolesPage rolesPage = new SecurityRolesPage(homePage);
            rolesPage.AddRoleGroup(roleGroup);

            //Act 
            rolesPage.FilterByGroupSelectList.Select(roleGroup);
            System.Threading.Thread.Sleep(2000);
            ConfirmDialogHandler dialog = new ConfirmDialogHandler();
            using (new UseDialogOnce(ieInstance.DialogWatcher, dialog))
            {
                rolesPage.DeleteRoleGroupButton.ClickNoWait();
                dialog.WaitUntilExists();
                dialog.OKButton.Click();
            }

            //Assert 
            System.Threading.Thread.Sleep(1000);
            Assert.IsFalse(rolesPage.FilterByGroupSelectList.Option(roleGroup).Exists);
        }

        [Test]
        [DependsOn("Add_New_Role_Group")]
        public void Edit_Role_Group()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            String roleGroup = "Test Group";

            //Create a role group
            ieInstance.GoTo(SiteURL + "/Admin/SecurityRoles.aspx");
            SecurityRolesPage rolesPage = new SecurityRolesPage(homePage);
            rolesPage.AddRoleGroup(roleGroup);

            //Act 
            rolesPage.FilterByGroupSelectList.Select(roleGroup);
            System.Threading.Thread.Sleep(2000);
            rolesPage.EditRoleGroupButton.Click();
            rolesPage.RoleGroupNameField.Value = "New Group Name";
            rolesPage.UpdateLink.Click();
            System.Threading.Thread.Sleep(1000);

            //Assert 
            Assert.IsTrue(rolesPage.FilterByGroupSelectList.Option("New Group Name").Exists);
            Assert.IsFalse(rolesPage.FilterByGroupSelectList.Option(roleGroup).Exists);
        }

        [Test]
        public void Add_Global_Role()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            String roleName = "Test Role";
            
            //Act 
            ieInstance.GoTo(SiteURL + "/Admin/SecurityRoles.aspx");
            SecurityRolesPage rolesPage = new SecurityRolesPage(homePage);
            rolesPage.AddNewRoleLink.Click();
            rolesPage.RoleNameField.Value = roleName;
            rolesPage.RoleGroupSelectList.Select("< Global Roles >");
            rolesPage.UpdateLink.ClickNoWait();
            System.Threading.Thread.Sleep(1500);

            //Assert 
            Assert.IsTrue(rolesPage.RolesTable.InnerHtml.Contains(roleName));
            Assert.IsTrue(rolesPage.GetRoleRow(roleName).Exists);
            TableCell publicCheckBoxCell = rolesPage.GetRoleRow(roleName).TableCells[10];
            TableCell autoCheckBoxCell = rolesPage.GetRoleRow(roleName).TableCells[11];
            Assert.IsTrue(publicCheckBoxCell.Image(Find.BySrc(s => s.Contains("Unchecked"))).Exists);
            Assert.IsTrue(autoCheckBoxCell.Image(Find.BySrc(s => s.Contains("Unchecked"))).Exists);
        }

        [Test]
        [DependsOn("Add_New_Role_Group")]
        public void Add_Role_To_Group()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            String roleGroup = "Test Group";
            String roleName = "Role 1";

            //Create a role group
            ieInstance.GoTo(SiteURL + "/Admin/SecurityRoles.aspx");
            SecurityRolesPage rolesPage = new SecurityRolesPage(homePage);
            rolesPage.AddRoleGroup(roleGroup);

            //Act 
            rolesPage.AddNewRoleLink.Click();
            rolesPage.RoleNameField.Value = roleName;
            rolesPage.RoleGroupSelectList.Select(roleGroup);
            rolesPage.UpdateLink.ClickNoWait();
            System.Threading.Thread.Sleep(3000);

            //Assert 
            Assert.IsFalse(rolesPage.RolesTable.InnerHtml.Contains(roleName));
            rolesPage.FilterByGroupSelectList.Select("< All Roles >");
            rolesPage.FilterByGroupSelectList.Refresh();
            System.Threading.Thread.Sleep(1500);
            Assert.IsTrue(rolesPage.RolesTable.InnerHtml.Contains(roleName));
            Assert.IsTrue(rolesPage.GetRoleRow(roleName).Exists);
            System.Threading.Thread.Sleep(1500);
            rolesPage.FilterByGroupSelectList.Select(roleGroup);
            System.Threading.Thread.Sleep(1000);
            Assert.IsTrue(rolesPage.RolesTable.InnerHtml.Contains(roleName));
            Assert.IsTrue(rolesPage.GetRoleRow(roleName).Exists);
        }


        [Test]
        [DependsOn("Add_Global_Role")]
        public void Add_Role_Auto_Assign_Public()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            String roleName = "Role 1";

            //Act 
            ieInstance.GoTo(SiteURL + "/Admin/SecurityRoles.aspx");
            SecurityRolesPage rolesPage = new SecurityRolesPage(homePage);
            rolesPage.AddNewRoleLink.Click();
            rolesPage.RoleNameField.Value = roleName;
            rolesPage.AutoAssignCheckBox.Checked = true;
            rolesPage.PublicRoleCheckBox.Checked = true;
            rolesPage.UpdateLink.Click();
            System.Threading.Thread.Sleep(2000);
            TableCell publicCheckBoxCell = rolesPage.GetRoleRow(roleName).TableCells[10];
            TableCell autoCheckBoxCell = rolesPage.GetRoleRow(roleName).TableCells[11];
            Assert.IsTrue(publicCheckBoxCell.Image(Find.BySrc(s => s.Contains("Checked"))).Exists);
            Assert.IsTrue(autoCheckBoxCell.Image(Find.BySrc(s => s.Contains("Checked"))).Exists);
            System.Threading.Thread.Sleep(1000);

            //Add a user to test if the role was added for them
            ieInstance.GoTo(SiteURL + "/Admin/UserAccounts.aspx");
            UserPage userPage = new UserPage(homePage);
            userPage.AddUser(TestUsers.User1.UserName, TestUsers.User1.Password, TestUsers.User1.FirstName, TestUsers.User1.LastName, TestUsers.User1.EmailAddress, TestUsers.User1.DisplayName);
            System.Threading.Thread.Sleep(1000);
            userPage.AllDisplayLink.Click();
            System.Threading.Thread.Sleep(1000);
            userPage.GetUserRolesButton(TestUsers.User1.DisplayName).Click();
            UserRolesPage userRoles = new UserRolesPage(homePage);

            //Assert 
            Assert.IsTrue(userRoles.UserRolesTable.InnerHtml.Contains(roleName));
        }

        [Test]
        [DependsOn("Add_Global_Role")]
        public void Test_Role_Effective_Date_Future()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            String roleName = "Role 1";
            String pageName = "Role Page 1";
            ControlPanel controlPanel = null;
            RibbonBar ribbonBar = null;
            DateTime date = DateTime.Now;
            int year = date.Year;
            year = year + 3;
            string dateString = date.Month + "/" + date.Day + "/" + year;

            //Add a page
            ieInstance.GoTo(SiteURL + "/Admin/Pages.aspx");
            AdminPagesPage pagesPage = new AdminPagesPage(homePage);
            pagesPage.AddPage(pageName);
            
            //Add a Role
            ieInstance.GoTo(SiteURL + "/Admin/SecurityRoles.aspx");
            SecurityRolesPage rolesPage = new SecurityRolesPage(homePage);
            rolesPage.AddRole(roleName);

            //Add a user
            ieInstance.GoTo(SiteURL + "/Admin/UserAccounts.aspx");
            UserPage userPage = new UserPage(homePage);
            userPage.AddUser(TestUsers.User1.UserName, TestUsers.User1.Password, TestUsers.User1.FirstName, TestUsers.User1.LastName, TestUsers.User1.EmailAddress, TestUsers.User1.DisplayName);

            //Add role permission to the page 
            ieInstance.GoTo(SiteURL + "/rolepage1.aspx");
            System.Threading.Thread.Sleep(2000);
            if (homePage.ControlPanelElement.Id.Contains(homePage.RibbonBarID))
            {
                ribbonBar = new RibbonBar(homePage);
                ribbonBar.CurrentPageTabLink.Click();
                ribbonBar.EditCurrentPageLink.Click();
            }
            else
            {
                controlPanel = new ControlPanel(homePage);
                controlPanel.PageSettingsLink.Click();
            }
            PageSettingsPage newPage = new PageSettingsPage(homePage);
            newPage.ClickViewPermissionForRole(roleName);
            newPage.AddPageLink.ClickNoWait();
            System.Threading.Thread.Sleep(1000);

            //Act 
            ieInstance.GoTo(SiteURL + "/Admin/UserAccounts.aspx");
            userPage.AllDisplayLink.Click();
            System.Threading.Thread.Sleep(1000);
            userPage.GetUserRolesButton(TestUsers.User1.DisplayName).Click();
            UserRolesPage userRoles = new UserRolesPage(homePage);
            userRoles.RoleSelectList.Select(roleName);
            userRoles.RoleSelectList.Refresh();
            userRoles.EffectiveDateField.Value = dateString;
            userRoles.AddRoleLink.ClickNoWait();
            System.Threading.Thread.Sleep(1000);

            //Assert 
            Assert.IsTrue(userRoles.UserRolesTable.InnerHtml.Contains(roleName));
            Assert.IsTrue(userRoles.UserRolesTable.InnerHtml.Contains(dateString));
            ieInstance.GoTo(SiteURL + "/Home.aspx");
            loginPage.LogoffUser();
            loginPage.LoginUser(TestUsers.User1.UserName, TestUsers.User1.Password);
            ieInstance.GoTo(SiteURL + "/rolepage1.aspx");
            Assert.IsTrue(homePage.ModuleMessageSpan.InnerHtml.Contains("You do not have access to view this tab within the portal."));
            Assert.IsFalse(homePage.ContentDiv.InnerHtml.Contains("Enter Title"));
        }

        [Test]
        [DependsOn("Add_Global_Role")]
        public void Test_Role_Effective_Date_Past()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            String roleName = "Role 1";
            String pageName = "Role Page 1";
            ControlPanel controlPanel = null;
            RibbonBar ribbonBar = null;
            DateTime date = DateTime.Now;
            int year = date.Year;
            year = year - 3;
            string dateString = date.Month + "/" + date.Day + "/" + year;

            //Add a page
            ieInstance.GoTo(SiteURL + "/Admin/Pages.aspx");
            AdminPagesPage pagesPage = new AdminPagesPage(homePage);
            pagesPage.AddPage(pageName);

            //Add a Role
            ieInstance.GoTo(SiteURL + "/Admin/SecurityRoles.aspx");
            SecurityRolesPage rolesPage = new SecurityRolesPage(homePage);
            rolesPage.AddRole(roleName);

            //Add a user
            ieInstance.GoTo(SiteURL + "/Admin/UserAccounts.aspx");
            UserPage userPage = new UserPage(homePage);
            userPage.AddUser(TestUsers.User1.UserName, TestUsers.User1.Password, TestUsers.User1.FirstName, TestUsers.User1.LastName, TestUsers.User1.EmailAddress, TestUsers.User1.DisplayName);

            //Add role permission to the page 
            ieInstance.GoTo(SiteURL + "/rolepage1.aspx");
            System.Threading.Thread.Sleep(2000);
            if (homePage.ControlPanelElement.Id.Contains(homePage.RibbonBarID))
            {
                ribbonBar = new RibbonBar(homePage);
                ribbonBar.CurrentPageTabLink.Click();
                ribbonBar.EditCurrentPageLink.Click();
            }
            else
            {
                controlPanel = new ControlPanel(homePage);
                controlPanel.PageSettingsLink.Click();
            }
            PageSettingsPage newPage = new PageSettingsPage(homePage);
            newPage.ClickViewPermissionForRole(roleName);
            newPage.AddPageLink.Click();
            System.Threading.Thread.Sleep(1000);

            //Act 
            ieInstance.GoTo(SiteURL + "/Admin/UserAccounts.aspx");
            userPage.AllDisplayLink.Click();
            System.Threading.Thread.Sleep(1000);
            userPage.GetUserRolesButton(TestUsers.User1.DisplayName).Click();
            UserRolesPage userRoles = new UserRolesPage(homePage);
            userRoles.RoleSelectList.Select(roleName);
            userRoles.EffectiveDateField.Value = dateString;
            userRoles.AddRoleLink.ClickNoWait();
            System.Threading.Thread.Sleep(1000);

            //Assert 
            Assert.IsTrue(userRoles.UserRolesTable.InnerHtml.Contains(roleName));
            Assert.IsTrue(userRoles.UserRolesTable.InnerHtml.Contains(dateString));
            ieInstance.GoTo(SiteURL + "/Home.aspx");
            loginPage.LogoffUser();
            loginPage.LoginUser(TestUsers.User1.UserName, TestUsers.User1.Password);
            ieInstance.GoTo(SiteURL + "/rolepage1.aspx");
            Assert.IsFalse(homePage.ModuleMessageSpan.Exists);
            Assert.IsFalse(homePage.ContentDiv.InnerHtml.Contains("You do not have access to view this tab within the portal."));
            Assert.IsTrue(homePage.ContentDiv.InnerHtml.Contains("Enter Title"));
        }

        [Test]
        [DependsOn("Add_Global_Role")]
        public void Test_Role_Expiry_Date_Future()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            String roleName = "Role 1";
            String pageName = "Role Page 1";
            ControlPanel controlPanel = null;
            RibbonBar ribbonBar = null;
            DateTime date = DateTime.Now;
            int year = date.Year;
            year = year + 3;
            string dateString = date.Month + "/" + date.Day + "/" + year;

            //Add a page
            ieInstance.GoTo(SiteURL + "/Admin/Pages.aspx");
            AdminPagesPage pagesPage = new AdminPagesPage(homePage);
            pagesPage.AddPage(pageName);

            //Add a Role
            ieInstance.GoTo(SiteURL + "/Admin/SecurityRoles.aspx");
            SecurityRolesPage rolesPage = new SecurityRolesPage(homePage);
            rolesPage.AddRole(roleName);

            //Add a user
            ieInstance.GoTo(SiteURL + "/Admin/UserAccounts.aspx");
            UserPage userPage = new UserPage(homePage);
            userPage.AddUser(TestUsers.User1.UserName, TestUsers.User1.Password, TestUsers.User1.FirstName, TestUsers.User1.LastName, TestUsers.User1.EmailAddress, TestUsers.User1.DisplayName);

            //Add role permission to the page 
            ieInstance.GoTo(SiteURL + "/rolepage1.aspx");
            System.Threading.Thread.Sleep(2000);
            if (homePage.ControlPanelElement.Id.Contains(homePage.RibbonBarID))
            {
                ribbonBar = new RibbonBar(homePage);
                ribbonBar.CurrentPageTabLink.Click();
                ribbonBar.EditCurrentPageLink.Click();
            }
            else
            {
                controlPanel = new ControlPanel(homePage);
                controlPanel.PageSettingsLink.Click();
            }
            PageSettingsPage newPage = new PageSettingsPage(homePage);
            newPage.ClickViewPermissionForRole(roleName);
            newPage.AddPageLink.Click();
            System.Threading.Thread.Sleep(1000);

            //Act 
            ieInstance.GoTo(SiteURL + "/Admin/UserAccounts.aspx");
            userPage.AllDisplayLink.Click();
            System.Threading.Thread.Sleep(1000);
            userPage.GetUserRolesButton(TestUsers.User1.DisplayName).Click();
            UserRolesPage userRoles = new UserRolesPage(homePage);
            userRoles.RoleSelectList.Select(roleName);
            userRoles.ExpiryDateField.Value = dateString;
            userRoles.AddRoleLink.Click();
            System.Threading.Thread.Sleep(1000);

            //Assert 
            Assert.IsTrue(userRoles.UserRolesTable.InnerHtml.Contains(roleName));
            Assert.IsTrue(userRoles.UserRolesTable.InnerHtml.Contains(dateString));
            ieInstance.GoTo(SiteURL + "/Home.aspx");
            loginPage.LogoffUser();
            loginPage.LoginUser(TestUsers.User1.UserName, TestUsers.User1.Password);
            ieInstance.GoTo(SiteURL + "/rolepage1.aspx");
            Assert.IsFalse(homePage.ModuleMessageSpan.Exists);
            Assert.IsFalse(homePage.ContentDiv.InnerHtml.Contains("You do not have access to view this tab within the portal."));
            Assert.IsTrue(homePage.ContentDiv.InnerHtml.Contains("Enter Title"));
        }

        [Test]
        [DependsOn("Add_Global_Role")]
        public void Test_Role_Expiry_Date_Past()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            String roleName = "Role 1";
            String pageName = "Role Page 1";
            ControlPanel controlPanel = null;
            RibbonBar ribbonBar = null;
            DateTime date = DateTime.Now;
            int year = date.Year;
            year = year - 3;
            string dateString = date.Month + "/" + date.Day + "/" + year;

            //Add a page
            ieInstance.GoTo(SiteURL + "/Admin/Pages.aspx");
            AdminPagesPage pagesPage = new AdminPagesPage(homePage);
            pagesPage.AddPage(pageName);

            //Add a Role
            ieInstance.GoTo(SiteURL + "/Admin/SecurityRoles.aspx");
            SecurityRolesPage rolesPage = new SecurityRolesPage(homePage);
            rolesPage.AddRole(roleName);

            //Add a user
            ieInstance.GoTo(SiteURL + "/Admin/UserAccounts.aspx");
            UserPage userPage = new UserPage(homePage);
            userPage.AddUser(TestUsers.User1.UserName, TestUsers.User1.Password, TestUsers.User1.FirstName, TestUsers.User1.LastName, TestUsers.User1.EmailAddress, TestUsers.User1.DisplayName);

            //Add role permission to the page 
            ieInstance.GoTo(SiteURL + "/rolepage1.aspx");
            System.Threading.Thread.Sleep(2000);
            if (homePage.ControlPanelElement.Id.Contains(homePage.RibbonBarID))
            {
                ribbonBar = new RibbonBar(homePage);
                ribbonBar.CurrentPageTabLink.Click();
                ribbonBar.EditCurrentPageLink.Click();
            }
            else
            {
                controlPanel = new ControlPanel(homePage);
                controlPanel.PageSettingsLink.Click();
            }
            PageSettingsPage newPage = new PageSettingsPage(homePage);
            newPage.ClickViewPermissionForRole(roleName);
            newPage.AddPageLink.Click();
            System.Threading.Thread.Sleep(1000);

            //Act 
            ieInstance.GoTo(SiteURL + "/Admin/UserAccounts.aspx");
            userPage.AllDisplayLink.Click();
            System.Threading.Thread.Sleep(1000);
            userPage.GetUserRolesButton(TestUsers.User1.DisplayName).Click();
            UserRolesPage userRoles = new UserRolesPage(homePage);
            userRoles.RoleSelectList.Select(roleName);
            userRoles.ExpiryDateField.Value = dateString;
            userRoles.AddRoleLink.Click();
            System.Threading.Thread.Sleep(1000);

            //Assert 
            Assert.IsTrue(userRoles.UserRolesTable.InnerHtml.Contains(roleName));
            Assert.IsTrue(userRoles.UserRolesTable.InnerHtml.Contains(dateString));
            ieInstance.GoTo(SiteURL + "/Home.aspx");
            loginPage.LogoffUser();
            loginPage.LoginUser(TestUsers.User1.UserName, TestUsers.User1.Password);
            ieInstance.GoTo(SiteURL + "/rolepage1.aspx");
            Assert.IsTrue(homePage.ModuleMessageSpan.InnerHtml.Contains("You do not have access to view this tab within the portal."));
            Assert.IsFalse(homePage.ContentDiv.InnerHtml.Contains("Enter Title"));
        }

        [Test]
        [DependsOn("Add_Global_Role")]
        [DependsOn("Add_Role_Auto_Assign_Public")]
        public void Test_Public_Role()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            String roleName = "Test Users";

            //Add a public role
            ieInstance.GoTo(SiteURL + "/Admin/SecurityRoles.aspx");
            SecurityRolesPage rolesPage = new SecurityRolesPage(homePage);
            rolesPage.AddNewRoleLink.Click();
            rolesPage.RoleNameField.Value = roleName;
            rolesPage.PublicRoleCheckBox.Checked = true;
            rolesPage.UpdateLink.ClickNoWait();
            System.Threading.Thread.Sleep(2000);

            //Add a user and assign the role to the new user
            ieInstance.GoTo(SiteURL + "/Admin/UserAccounts.aspx");
            UserPage userPage = new UserPage(homePage);
            userPage.AddUser(TestUsers.User1.UserName, TestUsers.User1.Password, TestUsers.User1.FirstName, TestUsers.User1.LastName, TestUsers.User1.EmailAddress, TestUsers.User1.DisplayName);
            System.Threading.Thread.Sleep(1000);
            userPage.AllDisplayLink.Click();
            System.Threading.Thread.Sleep(1000);
            userPage.GetUserRolesButton(TestUsers.User1.DisplayName).Click();
            UserRolesPage userRoles = new UserRolesPage(homePage);

            userRoles.RoleSelectList.Select(roleName);
            userRoles.AddRoleLink.ClickNoWait();
            System.Threading.Thread.Sleep(1000);

            //Act 
            ieInstance.GoTo(SiteURL + "/Home.aspx");
            System.Threading.Thread.Sleep(1500);
            loginPage.LogoffUser();
            System.Threading.Thread.Sleep(1500);
            loginPage.LoginUser(TestUsers.User1.UserName, TestUsers.User1.Password);
            loginPage.RegisterLink.Click();
            UserProfilePage profilePage = new UserProfilePage(homePage);
            profilePage.EditProfileLink.Click();
            profilePage.ManageServicesLink.Click();
            System.Threading.Thread.Sleep(1000);
            
            //Assert 
            Assert.IsTrue(profilePage.ServicesTable.InnerHtml.Contains(roleName));
        }

        [Test]
        [DependsOn("Add_New_Role_Group")]
        public void Add_Duplicate_Role_Group_Fail()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            String roleGroupName = "Test group";

            //Add a role group
            ieInstance.GoTo(SiteURL + "/Admin/SecurityRoles.aspx");
            SecurityRolesPage rolesPage = new SecurityRolesPage(homePage);
            System.Threading.Thread.Sleep(1500);
            rolesPage.AddRoleGroup(roleGroupName);

            //Act 
            rolesPage.AddRoleGroup(roleGroupName);

            //Assert 
            Assert.IsTrue(homePage.PopUpMessageSpan.InnerHtml.Contains("A role group with the same name already exists. The new group was not added."));
            ieInstance.GoTo(SiteURL + "/Admin/SecurityRoles.aspx");
            Assert.IsTrue(rolesPage.FilterByGroupSelectList.Options.Filter(Find.ByText(roleGroupName)).Count == 1);
        }

        [Test]
        [DependsOn("Add_New_Role_Group")]
        public void Add_Duplicate_Global_Role_Fail()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            String roleName = "Test Role";

            //Add a role group
            ieInstance.GoTo(SiteURL + "/Admin/SecurityRoles.aspx");
            SecurityRolesPage rolesPage = new SecurityRolesPage(homePage);
            rolesPage.AddRole(roleName);

            //Act 
            rolesPage.AddRole(roleName);

            //Assert 
            Assert.IsTrue(homePage.PopUpMessageSpan.InnerHtml.Contains("A role with the same name already exists. The role was not added."));
            ieInstance.GoTo(SiteURL + "/Admin/SecurityRoles.aspx");
            TableCellCollection roleCells = rolesPage.RolesTable.TableCells.Filter(Find.ByText(roleName));
            Assert.IsTrue(roleCells.Count == 1);
        }

        #endregion
    }
}

